<?php

/**
 * 系统设置控制器类
 * 
 * @package    01CMS
 * @subpackage admin
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class SettingController extends Controller
{
    public $title = '全局设置';

    function __construct ()
    {
        parent::__construct();
        
        $User = model('User');
        
        if (! $User->isLogined())
        {
            redirect('admin/login?url=' . getCurUrl());
        }
        
        if (! $User->checkAuth())
        {
            exit('您的权限不足');
        }
    }

    function vars ($action = '', $id = 0)
    {
        $this->title .= ' => 系统变量';
        
        if ($action == 'lists' && $id > 0)
        {
            $rows = model('Db')->getRows('select `name`, `value` from `@@__setting_vars`');
            
            $setting = array();
            
            foreach ($rows as $row)
            {
                $key = $row->name;
                
                $setting[$key] = $row->value;
            }
            
            lib('Write')->phpArray(SYS_PATH . '/config/var.php', $setting);
        }
        
        if ($action == 'update' || $action == 'insert')
        {
            $this->data = $this->Load->post('info,name|变量名不能为空,value|变量值不能为空,type');
            
            if ($error = $this->Load->getError())
            {
                response($error);
            }
        }
        
        $this->id = $id;
        $this->where = 'sort = 0';
        $this->action($action);
    }
    
    function ad ($action = '')
    {
        $this->title .= ' => 广告管理';
        
        if ($action == 'lists')
        {
            $this->data = data('adSetting');
            $this->Load->view('Smarty');
        }
        else if ($action == 'update')
        {
            $str = 'uploadPath|文件保存路径规则不能为空,allowedTypes,maxSize,thumbWidth,thumbHeight,thumbPadColor,thumbMarker,';
            $str .= 'width,height,wmText,wmType,wmFontSize,wmFontPath,wmFontColor,wmShadowColor,wmShadowDistance,wmShadowDistance,';
            $str .= 'wmVrtAlignment,wmHorAlignment,wmOpacity,wmPadding,wmOverlayPath,wmHorOffset,wmVrtOffset';
            
            $data = $this->Load->post($str);
            
            if ($error = $this->Load->getError())
            {
                response($error);
            }
            
            $sta = lib('Write')->phpArray(SYS_PATH . '/data/adSetting.php', $data);
            
            $message = ($sta > 0) ? '修改成功(' . $sta / 1000 . 'K)' : '修改失败';
            
            response($message);
        }
    }
    
    function link ($action = '')
    {
        $this->title .= ' => 友情链接';
        
        if ($action == 'lists')
        {
            $this->data = data('linkSetting');
            $this->Load->view('Smarty');
        }
        else if ($action == 'update')
        {
            $str = 'uploadPath|文件保存路径规则不能为空,allowedTypes,maxSize,thumbWidth,thumbHeight,thumbPadColor,thumbMarker,';
            $str .= 'width,height,wmText,wmType,wmFontSize,wmFontPath,wmFontColor,wmShadowColor,wmShadowDistance,wmShadowDistance,';
            $str .= 'wmVrtAlignment,wmHorAlignment,wmOpacity,wmPadding,wmOverlayPath,wmHorOffset,wmVrtOffset';
            
            $data = $this->Load->post($str);
            
            if ($error = $this->Load->getError())
            {
                response($error);
            }
            
            $sta = lib('Write')->phpArray(SYS_PATH . '/data/linkSetting.php', $data);
            
            $message = ($sta > 0) ? '修改成功(' . $sta / 1000 . 'K)' : '修改失败';
            
            response($message);
        }
    }
}

/* End of file /01CMS/application/controller/SettingController.php */